﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SPRepository
{
    public class ValueTypeFieldType<TF, TS> : SimpleFieldType<TF>
        where TS : struct
        where TF : class, IFieldValueTypeDto<TS>, new()
    {
        public ValueTypeFieldType(Guid id) : base(id)
        { }

        public override TF GetValue(Microsoft.SharePoint.SPListItem item)
        {
            var value = GetFieldRawValue(item);

            if (value == null)
            {
                return null;
            }
            else
            {
                return new TF() { Value = (TS)value };
            }
        }

        public override void SetValue(Microsoft.SharePoint.SPListItem item, TF value)
        {
            if (value == null)
            {
                SetFieldValue(item, null);
            }
            else
            {
                SetFieldValue(item, value.Value);
            }
        }
    }
}
